﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Furion.HttpRemote;
using Furion.Schedule;
using Microsoft.Extensions.Logging;
using MySqlConnector;
using SyncVideo.Core;
using static System.Net.Mime.MediaTypeNames;

namespace SyncVideo.Application.Jobs
{
    public class SyVideo : IJob
    {
        private readonly ILogger<SyVideo> _logger;
        private readonly IHttpRemoteService _httpRemoteService;
        public SyVideo(ILogger<SyVideo> logger, IHttpRemoteService httpRemoteService)
        {
            _logger = logger;
            _httpRemoteService = httpRemoteService;
        }

        public async Task ExecuteAsync(JobExecutingContext context, CancellationToken stoppingToken)
        {
            try
            {
                string videoBasePath = App.Configuration["VideoBasePath"];
                string serverIp = App.Configuration["ServerIp"];
                //string ssss = "5b37d94c-d323-4b17-b872-035272a72d85_LFZY_2025_05_28_031114.mp4";
                //var arry2 = ssss.Split('_');
                //if (arry2.Length != 6)
                //{
                //}
                //if (!int.TryParse(arry2[5], out int policeNumber2))
                //{
                   
                //}


                var list = await DbContext.Instance.Queryable<MediaEntity>().Where(p => string.IsNullOrEmpty(p.ext20) && p.type == "video").ToListAsync();
                _logger.LogInformation($"文件数量{list.Count}");
                _logger.LogInformation($"Sql:{DbContext.Instance.Queryable<MediaEntity>().Where(p => string.IsNullOrEmpty(p.ext20) && p.type == "video").ToSqlString()}");
                foreach (var item in list)
                {
                    try
                    {
                        if(item.size=="0")
                        {
                            _logger.LogInformation($"{item.id}文件大小为0");
                            item.ext20 = "4";
                            await DbContext.Instance.Updateable(item).ExecuteCommandAsync();
                            continue;
                        }
                        string path = videoBasePath + item.path;
                        if (!File.Exists(path))
                        {
                            _logger.LogInformation($"{item.id}文件不存在");
                            item.ext20 = "3";
                            await DbContext.Instance.Updateable(item).ExecuteCommandAsync();
                            continue;
                        }
                        string fileName = Path.GetFileNameWithoutExtension(path);
                        var arry = fileName.Split('_');
                        if (arry.Length != 6)
                        {
                            _logger.LogInformation($"{item.id}不复合上传要求");
                            item.ext20 = "2";
                            await DbContext.Instance.Updateable(item).ExecuteCommandAsync();
                            continue;
                        }
                        if (!int.TryParse(arry[5], out int policeNumber))
                        {
                            _logger.LogInformation($"{item.id}不复合上传要求");
                            item.ext20 = "2";
                            await DbContext.Instance.Updateable(item).ExecuteCommandAsync();
                            continue;
                        }
                        // 直接获取字符串内容
                        var res = await _httpRemoteService.PostAsAsync<ApiResponse>(serverIp + "/api/upload/upload-file", builder => builder.SetMultipartContent(multipart => multipart.AddFileAsStream(path, "file")));
                        if (res.succeeded)
                        {
                            item.ext20 = "1";
                            await DbContext.Instance.Updateable(item).ExecuteCommandAsync();
                            _logger.LogInformation($"{item.id}上传成功");
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError(ex.Message);
                        continue;
                    }
                }
            }
            catch (Exception ex)
            {

                _logger.LogError(ex.Message);
            }
        }
    }
    public class ApiResponse
    {
        public int statusCode { get; set; }
        public string data { get; set; }
        public bool succeeded { get; set; }
        public string errors { get; set; }
        public string extras { get; set; }
        public long timestamp { get; set; }
    }

}
